define(function (require, exports, module) {
    return function (jQuery) {
        (function ($) {
            /*seajs-content-start*/
            var homectrl = function ($scope, httpservice) {

                $scope.userlen={
                    diffvalue:"",
                    minvalue:"",
                    hour:""
                };

                $scope._23G_Len={
                    diffvalue:"",
                    minvalue:"",
                    hour:""
                };

                $scope._4G_Len={
                    diffvalue:"",
                    minvalue:"",
                    hour:""
                };

                $scope._4G_Count={
                    diffvalue:"",
                    minvalue:"",
                    hour:""
                };

                $scope._4G_Percent={
                    diffvalue:"",
                    minvalue:"",
                    hour:""
                };

                var dateFormat = function (date, fmt) { //author: meizz
                    var o = {
                        "M+": date.getMonth() + 1,                 //月份
                        "d+": date.getDate(),                    //日
                        "h+": date.getHours(),                   //小时
                        "m+": date.getMinutes(),                 //分
                        "s+": date.getSeconds(),                 //秒
                        "q+": Math.floor((date.getMonth() + 3) / 3), //季度
                        "S": date.getMilliseconds()             //毫秒
                    };
                    if (/(y+)/.test(fmt))
                        fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
                    for (var k in o)
                        if (new RegExp("(" + k + ")").test(fmt))
                            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
                    return fmt;
                };

                $scope.sumdate = dateFormat(new Date(new Date().setDate(new Date().getDate() - 1)),'yyyy-MM-dd');

                $scope.echart1 = {
                    chartdata:{
                        tooltip : {
                            formatter: "户均流量 : {c}MB"
                        },
//                        toolbox: {
//                            show : true,
//                            feature : {
//                                mark : {show: true},
//                                restore : {show: true},
//                                saveAsImage : {show: true}
//                            }
//                        },
                        series : [
                            {
                                name:'',
                                type:'gauge',
                                detail : {
                                    formatter:'{value}MB',
                                    textStyle: {       // 其余属性默认使用全局文本样式，详见TEXTSTYLE
                                        color: 'auto',
                                        fontWeight: 'normal',
                                        fontSize:20
                                    }},
                                min:0,
                                max:100,
                                data:[{value: 66, name: ''}],
                                radius: '90%',
                                axisLine: {            // 坐标轴线
                                    lineStyle: {       // 属性lineStyle控制线条样式
                                        color: [[0.2, '#2EC7C9'],[0.8, '#59B0EE'],[1, '#D8787F']],
                                        width: 5
                                    }
                                },
                                axisTick: {            // 坐标轴小标记
                                    splitNumber: 10,   // 每份split细分多少段
                                    length :8,        // 属性length控制线长
                                    lineStyle: {       // 属性lineStyle控制线条样式
                                        color: 'auto'
                                    }
                                },
                                splitLine: {           // 分隔线
                                    show: true,        // 默认显示，属性show控制显示与否
                                    length :12,         // 属性length控制线长
                                    lineStyle: {       // 属性lineStyle（详见lineStyle）控制线条样式
                                        color: 'auto'
                                    }
                                },
                                pointer : {
                                    width : 5
                                }
                            }
                        ]
                    },
                    obj:{}
                };

                $scope.echart2 = {
                    chartdata:{
                        tooltip : {
                            formatter: "2/3G总流量 : {c}TB"
                        },
//                        toolbox: {
//                            show : true,
//                            feature : {
//                                mark : {show: true},
//                                restore : {show: true},
//                                saveAsImage : {show: true}
//                            }
//                        },
                        series : [
                            {
                                name:'',
                                type:'gauge',
                                detail : {
                                    formatter:'{value}TB',
                                    textStyle: {       // 其余属性默认使用全局文本样式，详见TEXTSTYLE
                                        color: 'auto',
                                        fontWeight: 'normal',
                                        fontSize:20
                                    }},
                                min:0,
                                max:15,
                                data:[{value: 5, name: ''}],
                                radius: '90%',
                                axisLine: {            // 坐标轴线
                                    lineStyle: {       // 属性lineStyle控制线条样式
                                        color: [[0.2, '#2EC7C9'],[0.8, '#59B0EE'],[1, '#D8787F']],
                                        width: 5
                                    }
                                },
                                axisTick: {            // 坐标轴小标记
                                    splitNumber: 10,   // 每份split细分多少段
                                    length :8,        // 属性length控制线长
                                    lineStyle: {       // 属性lineStyle控制线条样式
                                        color: 'auto'
                                    }
                                },
                                splitLine: {           // 分隔线
                                    show: true,        // 默认显示，属性show控制显示与否
                                    length :12,         // 属性length控制线长
                                    lineStyle: {       // 属性lineStyle（详见lineStyle）控制线条样式
                                        color: 'auto'
                                    }
                                },
                                pointer : {
                                    width : 5
                                }
                            }
                        ]
                    },
                    obj:{}
                };

                $scope.echart3 = {
                    chartdata:{
                        tooltip : {
                            formatter: "4G总流量 : {c}TB"
                        },
//                        toolbox: {
//                            show : true,
//                            feature : {
//                                mark : {show: true},
//                                restore : {show: true},
//                                saveAsImage : {show: true}
//                            }
//                        },
                        series : [
                            {
                                name:'',
                                type:'gauge',
                                detail : {
                                    formatter:'{value}TB',
                                    textStyle: {       // 其余属性默认使用全局文本样式，详见TEXTSTYLE
                                        color: 'auto',
                                        fontWeight: 'normal',
                                        fontSize:20
                                    }},
                                min:0,
                                max:80,
                                data:[{value: 22, name: ''}],
                                radius: '90%',
                                axisLine: {            // 坐标轴线
                                    lineStyle: {       // 属性lineStyle控制线条样式
                                        color: [[0.2, '#2EC7C9'],[0.8, '#59B0EE'],[1, '#D8787F']],
                                        width: 5
                                    }
                                },
                                axisTick: {            // 坐标轴小标记
                                    splitNumber: 10,   // 每份split细分多少段
                                    length :8,        // 属性length控制线长
                                    lineStyle: {       // 属性lineStyle控制线条样式
                                        color: 'auto'
                                    }
                                },
                                splitLine: {           // 分隔线
                                    show: true,        // 默认显示，属性show控制显示与否
                                    length :12,         // 属性length控制线长
                                    lineStyle: {       // 属性lineStyle（详见lineStyle）控制线条样式
                                        color: 'auto'
                                    }
                                },
                                pointer : {
                                    width : 5
                                }
                            }
                        ]
                    },
                    obj:{}
                };

                $scope.echart4 = {
                    chartdata:{
                        tooltip : {
                            formatter: "4G用户数 : {c}万人"
                        },
//                        toolbox: {
//                            show : true,
//                            feature : {
//                                mark : {show: true},
//                                restore : {show: true},
//                                saveAsImage : {show: true}
//                            }
//                        },
                        series : [
                            {
                                name:'',
                                type:'gauge',
                                detail : {
                                    formatter:'{value}万人',
                                    textStyle: {       // 其余属性默认使用全局文本样式，详见TEXTSTYLE
                                        color: 'auto',
                                        fontWeight: 'normal',
                                        fontSize:20
                                    }},
                                min:0,
                                max:500,
                                data:[{value: 234, name: ''}],
                                radius: '90%',
                                axisLine: {            // 坐标轴线
                                    lineStyle: {       // 属性lineStyle控制线条样式
                                        color: [[0.2, '#2EC7C9'],[0.8, '#59B0EE'],[1, '#D8787F']],
                                        width: 5
                                    }
                                },
                                axisTick: {            // 坐标轴小标记
                                    splitNumber: 10,   // 每份split细分多少段
                                    length :8,        // 属性length控制线长
                                    lineStyle: {       // 属性lineStyle控制线条样式
                                        color: 'auto'
                                    }
                                },
                                splitLine: {           // 分隔线
                                    show: true,        // 默认显示，属性show控制显示与否
                                    length :12,         // 属性length控制线长
                                    lineStyle: {       // 属性lineStyle（详见lineStyle）控制线条样式
                                        color: 'auto'
                                    }
                                },
                                pointer : {
                                    width : 5
                                }
                            }
                        ]
                    },
                    obj:{}
                };

                $scope.echart5 = {
                    chartdata:{
                        tooltip : {
                            formatter: "4G用户渗透率 : {c}%"
                        },
//                        toolbox: {
//                            show : true,
//                            feature : {
//                                mark : {show: true},
//                                restore : {show: true},
//                                saveAsImage : {show: true}
//                            }
//                        },
                        series : [
                            {
                                name:'',
                                type:'gauge',
                                detail : {
                                    formatter:'{value}%',
                                    textStyle: {       // 其余属性默认使用全局文本样式，详见TEXTSTYLE
                                        color: 'auto',
                                        fontWeight: 'normal',
                                        fontSize:20
                                    }},
                                data:[{value: 88, name: ''}],
                                radius: '90%',
                                axisLine: {            // 坐标轴线
                                    lineStyle: {       // 属性lineStyle控制线条样式
                                        color: [[0.2, '#2EC7C9'],[0.8, '#59B0EE'],[1, '#D8787F']],
                                        width: 5
                                    }
                                },
                                axisTick: {            // 坐标轴小标记
                                    splitNumber: 10,   // 每份split细分多少段
                                    length :8,        // 属性length控制线长
                                    lineStyle: {       // 属性lineStyle控制线条样式
                                        color: 'auto'
                                    }
                                },
                                splitLine: {           // 分隔线
                                    show: true,        // 默认显示，属性show控制显示与否
                                    length :12,         // 属性length控制线长
                                    lineStyle: {       // 属性lineStyle（详见lineStyle）控制线条样式
                                        color: 'auto'
                                    }
                                },
                                pointer : {
                                    width : 5
                                }
                            }
                        ]
                    },
                    obj:{}
                };

                $scope.grid1 = {
                    gridpagedata:{
                        nowpage:1,
                        totalpage:20,
                        pagesize:15,
                        pagelist:[15,25,35],
                        toFirstpage: function(){
                            alert('首页');
                        },
                        toToppage: function(){
                            alert('上一页');
                        },
                        toNextpage: function(){
                            $scope.grid1.gridpagedata.nowpage++;
                            alert('下一页');
                        },
                        toLastpage: function(){
                            alert('最后一页');
                        },
                        topage: function(){
                            alert('第' + $scope.grid1.gridpagedata.nowpage + '页');
                        },
                        changePagesize: function(){
                            alert('切换到每页' + $scope.grid1.gridpagedata.pagesize + '条');
                        }
                    },
                    griddata: {
                        colnames: ['排名','热点关键字', '用户数', '搜索次数'],
                        colmodel: [
                            { name: 'ranking', index: 'ranking', sortable: false, sorttype: 'number', align:"center" },
                            { name: 'keyword', index: 'keyword', sortable: false, align:"center" },
                            { name: 'usercount', index: 'usercount', sortable: true, sorttype: 'number', align:"center"},
                            { name: 'searchcount', index: 'searchcount', sortable: true, sorttype: 'number',align:"center"}
                        ],
                        data: [
//                            { keyword: "邓紫棋演唱会", usercount:7883, searchcount:46738 },
//                            { keyword: "新浪总编离职", usercount:6784, searchcount:6738 },
//                            { keyword: "初中课本现广告", usercount:6783, searchcount:6875 },
//                            { keyword: "蓝翔校长现身", usercount:8742, searchcount:6008 },
//                            { keyword: "甄嬛传英文版", usercount:8768, searchcount:3000 }
                        ]
                    },
                    obj:{},
                    shrinkToFit:true,
                    gridresize: function (grid) {
                        grid.setGridWidth(($(window).width())*0.32);
                        grid.setGridHeight(130);
                    }
                };

                $scope.grid2 = {
                    gridpagedata:{
                        nowpage:1,
                        totalpage:20,
                        pagesize:15,
                        pagelist:[15,25,35],
                        toFirstpage: function(){
                            alert('首页');
                        },
                        toToppage: function(){
                            alert('上一页');
                        },
                        toNextpage: function(){
                            $scope.grid1.gridpagedata.nowpage++;
                            alert('下一页');
                        },
                        toLastpage: function(){
                            alert('最后一页');
                        },
                        topage: function(){
                            alert('第' + $scope.grid1.gridpagedata.nowpage + '页');
                        },
                        changePagesize: function(){
                            alert('切换到每页' + $scope.grid1.gridpagedata.pagesize + '条');
                        }
                    },
                    griddata: {
                        colnames: ['排名','热点网页', '浏览次数', '流量(MB)'],
                        colmodel: [
                            { name: 'ranking', index: 'ranking', sortable: false, sorttype: 'number', align:"center" },
                            { name: 'pv', index: 'pv', sortable: false, align:"center" },
                            { name: 'browse_count', index: 'browse_count', sortable: true, sorttype: 'number', align:"center"},
                            { name: 'total_len', index: 'total_len', sortable: true, sorttype: 'number',align:"center"}
                        ],
                        data: [
//                            { pv: "淘宝网", total_len:1000, browse_count:1000 },
//                            { pv: "百度", total_len:1000, browse_count:1000 },
//                            { pv: "腾讯", total_len:1000, browse_count:1000 },
//                            { pv: "天猫", total_len:1000, browse_count:1000 },
//                            { pv: "京东商城", total_len:1000, browse_count:1000 }
                        ]
                    },
                    obj:{},
                    shrinkToFit:true,
                    gridresize: function (grid) {
                        grid.setGridWidth(($(window).width())*0.32);
                        grid.setGridHeight(130);
                    }
                };

                $scope.grid3 = {
                    gridpagedata:{
                        nowpage:1,
                        totalpage:20,
                        pagesize:15,
                        pagelist:[15,25,35],
                        toFirstpage: function(){
                            alert('首页');
                        },
                        toToppage: function(){
                            alert('上一页');
                        },
                        toNextpage: function(){
                            $scope.grid1.gridpagedata.nowpage++;
                            alert('下一页');
                        },
                        toLastpage: function(){
                            alert('最后一页');
                        },
                        topage: function(){
                            alert('第' + $scope.grid1.gridpagedata.nowpage + '页');
                        },
                        changePagesize: function(){
                            alert('切换到每页' + $scope.grid1.gridpagedata.pagesize + '条');
                        }
                    },
                    griddata: {
                        colnames: ['排名','APP应用', '使用人数', '流量(MB)'],
                        colmodel: [
                            { name: 'ranking', index: 'ranking', sortable: false, sorttype: 'number', align:"center" },
                            { name: 'app', index: 'app', sortable: false, align:"center" },
                            { name: 'usercount', index: 'usercount', sortable: true, sorttype: 'number', align:"center"},
                            { name: 'totallen', index: 'totallen', sortable: true, sorttype: 'number',align:"center"}
                        ],
                        data: [
//                            { app: "微信", usercount:1000, totallen:1000 },
//                            { app: "腾讯QQ", usercount:1000, totallen:1000 },
//                            { app: "微博", usercount:1000, totallen:1000 },
//                            { app: "支付宝", usercount:1000, totallen:1000 },
//                            { app: "百度地图", usercount:1000, totallen:1000 }
                        ]
                    },
                    obj:{},
                    shrinkToFit:true,
                    gridresize: function (grid) {
                        grid.setGridWidth(($(window).width())*0.32);
                        grid.setGridHeight(130);
                    }
                };

                var datenow = dateFormat(new Date(new Date().setDate(new Date().getDate() - 1)),'yyyy-MM-dd');
                var query = "date="+datenow+"";
                $scope.init=function(){
                    var f1 = httpservice.getJSON("script/OverView/GetKeyWordSort",query,function(data){
                        $scope.grid1.griddata.data = data.datas;
                        $scope.$apply();
                    });

                    var f2 = httpservice.getJSON("script/OverView/GetPVSort",query,function(data){
                        $scope.grid2.griddata.data = data.datas;
                        $scope.$apply();
                    });

                    var f3 =httpservice.getJSON("script/OverView/GetAPPSort",query,function(data){
                        $scope.grid3.griddata.data = data.datas;
                        $scope.$apply();
                    });

                    var ff1 = httpservice.getJSON("script/OverView/GetAvgUserIplen",query,function(result){
                        if(result.data == null){
                            $scope.echart1.chartdata.series[0].data[0].value = 0;
                            $scope.userlen={
                                diffvalue:"",
                                minvalue:"",
                                hour:""
                            };
                        }
                        else{
                            $scope.echart1.chartdata.series[0].data[0].value = result.data.day_avg_user_iplen;
                            $scope.userlen={
                                diffvalue:result.data.diff_value<0?Math.abs(result.data.diff_value):result.data.diff_value,
                                minvalue:result.data.min_ip_len,
                                hour:result.data.hour+":00"
                            };
                            if(result.data.diff_value > 0){
                                $('#bsy1').append('<img src="../css/image/u159.jpg">');
                            }
                            else if(result.data.diff_value < 0){
                                $('#bsy1').append('<img src="../css/image/u161.jpg">');
                            }
                            else{}
                        }
                        $scope.$apply();
                    });

                    var ff2 = httpservice.getJSON("script/OverView/Get23gIplen",query,function(result){
                        if(result.data == null){
                            $scope.echart2.chartdata.series[0].data[0].value = 0;
                            $scope._23G_Len={
                                diffvalue:"",
                                minvalue:"",
                                hour:""
                            };
                        }
                        else{
                            $scope.echart2.chartdata.series[0].data[0].value = result.data.day_23g_iplen;
                            $scope._23G_Len={
                                diffvalue:result.data.diff_value<0?Math.abs(result.data.diff_value):result.data.diff_value,
                                minvalue:result.data.min_ip_len,
                                hour:result.data.hour+":00"
                            };
                            if(result.data.diff_value > 0){
                                $('#bsy2').append('<img src="../css/image/u159.jpg">');
                            }
                            else if(result.data.diff_value < 0){
                                $('#bsy2').append('<img src="../css/image/u161.jpg">');
                            }
                            else{}
                        }
                        $scope.$apply();
                    });

                    var url = '192.168.4.98:8081/api/getName?id=990&name=ppo';
                    var ff3 = httpservice.getJSON("script/OverView/Get4gIplen",query,function(result){
                        if(result.data == null){
                            $scope.echart3.chartdata.series[0].data[0].value = 0;
                            $scope._4G_Len={
                                diffvalue:"",
                                minvalue:"",
                                hour:""
                            };
                        }
                        else
                        {
                            $scope.echart3.chartdata.series[0].data[0].value = result.data.day_4g_iplen;
                            $scope._4G_Len={
                                diffvalue:result.data.diff_value<0?Math.abs(result.data.diff_value):result.data.diff_value,
                                minvalue:result.data.min_ip_len,
                                hour:result.data.hour+":00"
                            };
                            if(result.data.diff_value > 0){
                                $('#bsy3').append('<img src="../css/image/u159.jpg">');
                            }
                            else if(result.data.diff_value < 0){
                                $('#bsy3').append('<img src="../css/image/u161.jpg">');
                            }
                            else{}
                        }
                        $scope.$apply();
                    });

                    var ff4 = httpservice.getJSON("script/OverView/Get4gUserCount",query,function(result){
                        if(result.data == null){
                            $scope.echart4.chartdata.series[0].data[0].value = 0;
                            $scope._4G_Count={
                                diffvalue:"",
                                minvalue:"",
                                hour:""
                            };
                        }
                        else{
                            $scope.echart4.chartdata.series[0].data[0].value = result.data.day_imsi_count;
                            $scope._4G_Count={
                                diffvalue:result.data.diff_value<0?Math.abs(result.data.diff_value):result.data.diff_value,
                                minvalue:result.data.min_imsi_count,
                                hour:result.data.hour+":00"
                            };
                            if(result.data.diff_value > 0){
                                $('#bsy4').append('<img src="../css/image/u159.jpg">');
                            }
                            else if(result.data.diff_value < 0){
                                $('#bsy4').append('<img src="../css/image/u161.jpg">');
                            }
                            else{}
                        }
                        $scope.$apply();
                    });

                    var ff5 = httpservice.getJSON("script/OverView/Get4gPercent",query,function(result){
                        if(result.data == null){
                            $scope.echart5.chartdata.series[0].data[0].value = 0;
                            $scope._4G_Percent={
                                diffvalue:"",
                                minvalue:"",
                                hour:""
                            };
                        }
                        else{
                            $scope.echart5.chartdata.series[0].data[0].value = result.data.day_proportion;
                            $scope._4G_Percent={
                                diffvalue:result.data.diff_value<0?Math.abs(result.data.diff_value)+"%":result.data.diff_value+"%",
                                minvalue:result.data.min_proportion+'%',
                                hour:result.data.hour+":00"
                            };
                            if(result.data.diff_value > 0){
                                $('#bsy5').append('<img src="../css/image/u159.jpg">');
                            }
                            else if(result.data.diff_value < 0){
                                $('#bsy5').append('<img src="../css/image/u161.jpg">');
                            }
                            else{}
                        }
                        $scope.$apply();
                    });


                    $scope.$emit('loading', 'true');
                    //$scope.$apply();
                    $.when(f1,f2,f3,ff1,ff2,ff3,ff4,ff5).then(function () {

                    }).always(function () {
                            $scope.$emit('loading', 'false');
                            $scope.$apply();
                        });
                };

                $scope.init();
            };
            angular.module('home-ctrl', [])
                .controller('HomeCtrl', ['$scope', 'HttpService', homectrl]);
            /*seajs-content-end*/
        })(jQuery);
    }
});